package cn.chenc.framework.demo.config;

import cn.chenc.framework.core.filter.XssFilter;
import cn.chenc.framework.demo.interceptor.MyInterceptor;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.HashMap;
import java.util.Map;

/**
 * 　@description: TODO
 * 　@author 陈_C
 * 　@date 2020/3/13 23:37
 *
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    /**
     * 代码方式注册过滤器Bean
     * @return
     */
    @Bean
    public FilterRegistrationBean xssFilterRegistration(){
        FilterRegistrationBean filterBean = new FilterRegistrationBean();
        filterBean.setFilter(new XssFilter());
        filterBean.setName("xssFilter");
        filterBean.addUrlPatterns("/*");
        Map<String, String> initParameters = new HashMap<String, String>();
//        initParameters.put("excludes", null);
//        initParameters.put("enabled", null);
        filterBean.setInitParameters(initParameters);
        return filterBean;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**")
                //需要配置2：----------- 告知拦截器：/static/admin/** 与 /static/user/** 不需要拦截 （配置的是 路径）
                .excludePathPatterns("/swagger-resources/**",
                        "/webjars/**", "/v2/**","swagger-ui.html",
                        "/swagger-ui.html/**",
                        "/static/**",
                        "/css/**",
                        "/data/**",
                        "/images/**",
                        "/js/**",
                        "/lib/**",
                        "classpath:/static/");

    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/");
    }



}
